<template>
  <el-drawer
    title="编辑"
    :visible.sync="visible"
    :before-close="handleClose"
    :destroy-on-close="true"
    :wrapperClosable="false"
    :size="this.drawerSize"
  >
    <div class="drawer-content">
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="100px"
        :destroy-on-close="true"
      >
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="地址" prop="href">
          <el-input v-model="form.href"></el-input>
        </el-form-item>
        <el-form-item label="排序">
          <el-input v-model="form.qOrder"></el-input>
        </el-form-item>
        <el-form-item label="是否显示" prop="out">
          <el-switch v-model="form.showFlag" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleAdd">确定</el-button>
          <el-button @click="handleClose">取消</el-button>
        </el-form-item>
      </el-form>
    </div>
  </el-drawer>
</template>

<script>
import { DRAWER_SIZE_BIG } from "@/util/constant";
import {
  getQuickDetailApi,
  updateQuickApi,
} from "@/api/modules/websites/quick.api";

export default {
  data() {
    return {
      visible: false,
      form: {
        id: '',
        name: "",
        href: "",
        qOrder: 1,
        showFlag: true,
      },
      drawerSize: DRAWER_SIZE_BIG,
      rules: {
        name: [{ required: true, message: "请输入", trigger: "blur" }],
        href: [{ required: true, message: "请输入", trigger: "blur" }],
      },
    };
  },

  methods: {
    async init(id) {
      const res = await getQuickDetailApi(id);
      this.form.id = res.id;
      this.form.name = res.name;
      this.form.href = res.href;
      this.form.showFlag = res.showFlag;
      this.form.qOrder = res.qOrder;
      this.visible = true;
    },
    handleClose() {
      this.visible = false;
    },
    async handleAdd() {
      this.$refs.form.validate(async (valid) => {
        if(valid){
          await updateQuickApi(this.form);
          this.$message({
            message: "成功",
            type: "success",
          });
          this.visible = false;
          this.$emit("getParentTableData");
        }
      })
    },
  },
};
</script>
